package com.jiangroom.hello.web;

import com.jiangroom.hello.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dengzaiqiang
 * @createTime 2018/4/9
 * @description hello
 */
@RestController
public class HelloController {

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/hello")
    public String index() {
        List<ServiceInstance> instanceList = client.getInstances("hello-service");
        if (CollectionUtils.isEmpty(instanceList)) {
            System.out.println("have not instance");
        } else {
            System.out.println("service_id=" + instanceList.get(0).getServiceId());
        }

        return "Hello World!";
    }

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") Long id){
        return new User(id,"test");
    }

    @PostMapping("/updateUser")
    public void update(User user){
        System.out.println(user);
    }


    /**
     * 测试feign
     */
    @RequestMapping(value = "/hello1", method = RequestMethod.GET)
    public String hello(@RequestParam String name) {
        return "Hello " + name;
    }

    @RequestMapping(value = "/hello2", method = RequestMethod.GET)
    public User hello(@RequestHeader String name, @RequestHeader Long id) {
        return new User(id, name);
    }

    @RequestMapping(value = "/hello3", method = RequestMethod.POST)
    public String hello(@RequestBody User user) {
        return user.toString();
    }
}
